// pages/helpdetail/helpdetail.js
Page({

  data: {
    helpinfoid:"",
    helpinfo:[],
    content:'',
    comment:[],
    personaltarget:'none',
    collect:"",
    // 个人信息
    wxAva:"",
    userName:"",
    academy:"",
    userClass:"",
    contactPhone:"",
    judgenum:0
  },
  onLoad(options){
    // console.log(options)
    var that = this;
    this.setData({
      helpinfoid:options.helpinfoid
    })

    wx.request({
      url: 'http://127.0.0.1:8080/helpInfo/helpInfoById',
      method:"POST",
      data:{helpinfoid:options.helpinfoid},
      success(res){
        // console.log(res)
        
        res.data[0].helpPic = JSON.parse(res.data[0].helpPic)
        that.setData({
          helpinfo:res.data
        })
        
      
        wx.request({
          url: 'http://127.0.0.1:8080/helpCollect/isCollect',
          method:"POST",
          data:{
            helpinfoid:options.helpinfoid,
            userid:wx.getStorageSync('openid')
          },
          success(res){
            if(res.data.length != 0){
             that.setData({
               collect:true
             })
              // console.log(res.data[0] !="")
            }
            // console.log(res)
          }
        })

        //增加浏览量
       var see = parseInt(res.data[0].helpInfoSee) + 1;
      //  console.log(see)
       wx.request({
         url: 'http://127.0.0.1:8080/helpInfo/addInfoSee',
         method:"POST",
         data:{
           helpinfoid:options.helpinfoid,
           seenum:see
         },
         success(res){
           if(res.data){
             var helpinfo = 'helpinfo[0].helpInfoSee';
             that.setData({
                [helpinfo]: see
             })
           }
         }
       })

      }
    })
    
    that.showjudge(options.helpinfoid);

  },
  onShow(){
  },
  //渲染评论函数
  showjudge(helpinfoid){
    var that =this
      //渲染评论
      wx.request({
        url: 'http://127.0.0.1:8080/helpInfoJudge/selectJudgeById',
        method:"POST",
        data:{helpinfoid:helpinfoid},
        success(res){
          that.setData({
            comment:res.data,
            judgenum:res.data.length,
            content:""
          })
          // console.log(res)
        }
      })
  },

  showperson(e){
    if(wx.getStorageSync('isuser') == "yes"){
      // console.log(e)
    var index = e.currentTarget.dataset.index;
    var user = this.data.helpinfo[index]
    // console.log(user)
    this.setData({
      wxAva:user.wxAva,
      userName:user.userName,
      academy:user.academy,
      userClass:user.userClass,
      contactPhone:user.phone,
      personaltarget:false
    })
    }else{
      wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
    
  },
  cancelperson(e){
    this.setData({
      personaltarget:'none'
    })
  },
    // 查看图片
  showpic(e){
      var index  =e.currentTarget.dataset.index;
      wx.previewImage({
        current: this.data.helpinfo[0].helpPic[index],     //当前图片地址
        urls: this.data.helpinfo[0].helpPic,                 //所有要预览的图片的地址集合 数组形式
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    },

  /*获得输入评论 */
  getcomment(e){
    // console.log(e.detail.value)
    this.setData({
      content:e.detail.value
    })
  },
  //提交该用户评论
  comment(){
    if(wx.getStorageSync('isuser') == "yes"){
      var that = this;
    // console.log(that.data.helpinfoid)
    // console.log(new Date().toJSON()) 标准时间json化再用substring取中间一段
    var nowtime = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8);
    var data = {
      helpjudgetext:this.data.content,
      helpjudgetime:nowtime,
      userid:wx.getStorageSync('openid'),
      infoid:that.data.helpinfoid
    }
    // console.log(data)
    wx.request({
      url: 'http://127.0.0.1:8080/helpInfoJudge/addHelpJudgeInfo',
      method:"POST",
      data:data,
      success(res){
        if(res.data){
          that.showjudge(that.data.helpinfoid);
        }
      }
    })
    }else{
      wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
    

  },
  collect(e){
    if(wx.getStorageSync('isuser') == "yes"){
      var that = this;
    var text = this.data.collect;
    this.setData({
      collect:!text
    })
    // console.log(this.data.collect)
    if(this.data.collect){
      var nowtime = new Date().toJSON().substring(0,10)+' '+new Date().toTimeString().substring(0,8);
      var data = {
        helpcollecttime:nowtime,
        userhelpcollect:wx.getStorageSync('openid'),
        helpinfoid:that.data.helpinfoid,
      }
      // console.log(data)
      wx.request({
        url: 'http://127.0.0.1:8080/helpCollect/addHelpCollect',
        method:"POST",
        data:data,
        success(res){
          if(res.data){
            wx.showToast({
              title: '收藏成功',
              duration: 2000,
              icon: "none",
              success: (res) => {},
            })
          }
          console.log(res)
        }
      })
    }else{
      var data = {
        userhelpcollect:wx.getStorageSync('openid'),
        helpinfoid:that.data.helpinfoid,
      }
      // console.log(data)
      wx.request({
        url: 'http://127.0.0.1:8080/helpCollect/delHelpCollect',
        method:"POST",
        data:data,
        success(res){
          // console.log(res)
        }
      })
    }
    }else{
      wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
    
  }

})